package com.kh.pajx.sop.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.IHsEquOnlineDao;
import com.kh.pajx.sop.domain.HsEquOnline;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.kh.pajx.sop.util.page.QueryHelperForManyTable;

@Repository
public class HsEquOnlineDaoImpl extends BaseDaoImpl<HsEquOnline> implements
		IHsEquOnlineDao {



	@Override
	public List getByJdbcSQL(String sql) {
		List pojolist = new ArrayList();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;
		Session session = null;
		session = getSession().getSessionFactory().openSession();
		con = session.connection();
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			rsmd = rs.getMetaData();
			int count = rsmd.getColumnCount();
			while (rs.next()) {
				List temp_entity = new ArrayList();
				for (int i = 1; i <= count; i++) {
					temp_entity.add(rs.getString(i));
				}
				pojolist.add(temp_entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (session != null) {
					session.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pojolist;
	}

	@Override
	public void addEquOnline() {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Connection con = null;
		Session session = null;
		session = getSession().getSessionFactory().openSession();
		con = session.connection();
		CallableStatement cst = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			cst = con.prepareCall("{call PROC_EQU_ONLINE(?)}");
			cst.setString(1, sdf.format(new Date()));
			cst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			try {
				if (con != null) {
					con.close();
				}
				if (cst != null) {
					cst.close();
				}
				if (session != null) {
					session.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public List findEquOnline(String sclId,String equType,long equNo,long date ) {
		String sql = "select * from HS_EQU_ONLINE where to_number(MONITOR_TIME) >= " +date ;
		if(equNo>0){
			sql+=" and EQU_NO = "+equNo +" order by to_number(MONITOR_TIME)ASC ";
		}else{
			sql+=" and EQU_NO in (select EQU_NO from HS_EQUIPMENT_INFO where SCL_ID = '"+sclId+"' and EQU_TYPE = '"+equType+"' and EQU_STATUS_FLAG='1')";
		}
		
		/*String sql = "";
		if(equNo > 0){
			sql += "select * from hs_equ_online o,hs_equipment_info e where "
					+ "o.equ_no = e.equ_no and  o.monitor_time >= '"+date+"' and o.equ_no='"+equNo+"' "
							+ "and e.equ_status_flag='1' order by o.monitor_time asc";
		}else{
			sql+="select * from HS_EQU_ONLINE where to_number(MONITOR_TIME) >= " +date ;
			sql+=" and EQU_NO in (select EQU_NO from HS_EQUIPMENT_INFO where SCL_ID = '"+sclId+"' and EQU_TYPE = '"+equType+"' and EQU_STATUS_FLAG='1')";
		}*/
		return this.getByJdbcSQL(sql);
	}

}
